package com.example.mobile2121606109ycg0428;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class KeyButton extends View {
    private String text="";
    private int textSize=100;

    public KeyButton(Context context) { super(context); }
    public KeyButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.KeyButton);
        text=typedArray.getString(R.styleable.KeyButton_myText);
        textSize= Integer.valueOf(typedArray.getString(R.styleable.KeyButton_myTextSize));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.colorKeyButton));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        canvas.drawRoundRect(1,1,width,height,10,10,paint);

        paint.setTextSize(textSize);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics metrics = paint.getFontMetrics();
        canvas.drawText(text,width/2,height/2-metrics.top/2-metrics.bottom/2,paint);
    }
}
